<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 代码生成
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.tableName" placeholder="表名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="tableName" label="表名"></el-table-column>
                <el-table-column prop="tableComment" label="表描述"></el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >生成</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-tabs v-model="activeName" >
                    <el-tab-pane label="后台配置" name="first">
                        <el-form-item label="表名" prop="tableName" >
                            <el-input v-model="form.tableName"></el-input>
                        </el-form-item>
                        <el-form-item label="表描述" >
                            <el-input v-model="form.tableComment"></el-input>
                        </el-form-item>
                        <el-form-item label="作者">
                            <el-input v-model="form.author"></el-input>
                        </el-form-item>
                        <el-form-item label="模块名">
                            <el-input v-model="form.moduleName"></el-input>
                        </el-form-item>
                        <el-form-item label="包信息">
                            <el-input v-model="form.packageInfo"></el-input>
                        </el-form-item>
                        <el-form-item label="类名">
                            <el-input v-model="form.className"></el-input>
                        </el-form-item>
                        <el-form-item label="输出">
                            <el-input v-model="form.outputDir"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="前端配置" name="second">
                        <el-table
                                :data="columnList"
                                stripe
                                style="width: 100%">
                            <el-table-column
                                    prop="columnName"
                                    label="字段名称"
                                    width="80">
                            </el-table-column>
                            <el-table-column
                                    prop="dataType"
                                    label="字段类型"
                                    width="80">
                            </el-table-column>
                            <el-table-column
                                    prop="columnComment"
                                    label="字段描述">
                            </el-table-column>
                            <el-table-column
                                    prop="tableChecked"
                                    label="列表">
                                <template slot-scope="scope">
                                    <el-checkbox v-model="scope.row.tableChecked"></el-checkbox>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="formChecked"
                                    label="表单">
                                <template slot-scope="scope">
                                    <el-checkbox v-model="scope.row.formChecked"></el-checkbox>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="columnType"
                                    label="表单类型">
                                <template slot-scope="scope">
                                    <el-select v-model="scope.row.type" size="mini" placeholder="请选择" style="width:100px;">
                                        <el-option label="文本框" value="1"></el-option>
                                        <el-option label="文本域" value="2"></el-option>
                                        <el-option label="下拉框" value="3"></el-option>
                                        <el-option label="日期框" value="4"></el-option>
                                        <el-option label="封面图" value="5"></el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="searchChecked"
                                    label="查询">
                                <template slot-scope="scope">
                                    <el-checkbox v-model="scope.row.searchChecked"></el-checkbox>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="searchType"
                                    label="查询类型">
                                <template slot-scope="scope">
                                    <el-select v-model="scope.row.searchType" size="mini" placeholder="请选择" style="width:100px;">
                                        <el-option label="==" value="1"></el-option>
                                        <el-option label="like" value="2"></el-option>
                                        <el-option label=">= && <" value="3"></el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { genPage, genInfo, genGenerate } from '../../api/gen';

export default {
    name: 'login-log-list',
    data() {
        return {
            // 遮罩层
            loading: true,
            query: {
                jobName: '',
                current: 1,
                size: 10
            },
            editVisible:false,
            form:{
                author:"",
                tableName:"",
                moduleName:"",
                packageInfo:"",
                className:"",
                outputDir:"",
                tableComment:"",
                columnJson:""
            },
            columnList:[],
            activeName: 'first',
            tableData: [],
            pageTotal: 0,
            rules: {
                tableName: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.loading = true;
            genPage(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
                this.pageTotal = res.count;
            });
        },
        handleEdit(index, row) {
            this.form.tableName = row.tableName;
            genInfo(row.tableName).then(res => {
                // 赋值
                this.form.author = res.tableInfo.author;
                this.form.moduleName = res.tableInfo.moduleName;
                this.form.packageInfo = res.tableInfo.packageInfo;
                this.form.className = res.tableInfo.className;
                this.form.tableComment = res.tableInfo.tableComment;
                this.form.outputDir = res.tableInfo.outputDir;
                this.columnList = res.columnList;
            });
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                let columnList = this.columnList;
                let columnJson = JSON.stringify(columnList);
                this.form.columnJson = columnJson;
                if (this.form.tableName) {
                    genGenerate(this.form).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`生成成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    },
    filters:{
        showStatus(status){
            if (status == "0") {
                return "成功";
            } else if (status == "1") {
                return "失败";
            } else {
                return "";
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
